import { applyDecorators } from "@nestjs/common";
import { ApiProperty, ApiPropertyOptional, PartialType as PartialTypeFromSwagger } from "@nestjs/swagger";
import { Exclude, Type } from "class-transformer";
import { IsBoolean, IsEmail, IsNotEmpty, IsNumber, IsOptional, IsString, MaxLength, MinLength } from "class-validator";
import { i18nValidationMessage } from "nestjs-i18n";
import { PartialType, OmitType } from '@nestjs/mapped-types';
export class CreateUserDto{
    @IsString()
    @ApiProperty({description:'用户名', example:'nick'})
    username: string;

    @PasswrodValidators()
    @ApiProperty({description:'密码',  example:'123123'})
    password: string;

    @IsString()
    @IsOptional() // 参数可选
    @ApiPropertyOptional() // 文档可选
    @ApiProperty({description:'手机号',  example:'13122222222'})
    mobile: string;

    @IsEmail()
    @IsString()
    @ApiProperty({description:'邮箱',  example:'21321@qq.com'})
    @IsOptional()
    @IsNotEmpty({message: i18nValidationMessage('validation.isNotEmpty',{field: 'email'})})
    email: string;

    @Type(() => Number) // 强类型转换
    @ApiProperty({description:'生效状态',  example:1})
    @IsOptional()
    status: number;

    @IsBoolean()
    @Type(() => Boolean) // 强类型转换
    @ApiProperty({description:'是否超级管理员', example:true})
    @IsOptional()
    is_super: boolean;

    @IsNumber()
    @ApiProperty({description:'排序号', example:100})
    @IsOptional()
    sort: number;
}

export class UpdateUserDto extends PartialType(CreateUserDto){
    @ApiProperty({description:'用户Id', example:1})
    @IsOptional()
    @IsNumber()
    id: number;
    
    @PasswrodValidators()
    @IsOptional()
    @ApiProperty({description:'密码',  example:'123123'})
    password?: string;
}

function PasswrodValidators(){
    return applyDecorators(
        IsString(),
        IsNotEmpty({message: i18nValidationMessage('validation.isNotEmpty',{field: 'password'})}),
        MinLength(6,{message: i18nValidationMessage('validation.minLength',{field: 'password', length: 6})}), 
        MaxLength(8, {message: i18nValidationMessage('validation.maxLength',{field: 'password', length: 8})})
    )
}

export class UpdateUserRolesDto {
    readonly roleIds: number []
}